/**
 * Created by gongxun on 16/12/22.
 */

var HallWaitLayer = cc.Layer.extend({
    mBg : null,
    mLoadingSprite : null,
    mLoaddingAnim : null,
    ctor : function(){
        this._super();

        this.mBg = new cc.LayerColor(cc.color(0, 0, 0, 140), mo.curSize.x, mo.curSize.y);
        this.addChild(this.mBg);

        if (mo.gameHelper.projectCode == 0){
            cc.spriteFrameCache.addSpriteFrames("res/loading.plist", "res/loading.png");

            this.mLoadingSprite = new cc.Sprite("#loding_0.png");
            this.mLoadingSprite.setPosition(cc.p(mo.curSize.x / 2, mo.curSize.y / 2));
            this.addChild(this.mLoadingSprite);
        }else{
            this.mLoadingSprite = new cc.Sprite("res/ccs/hall_second/login/loadinglogo.png");
            this.mLoadingSprite.setPosition(cc.p(mo.curSize.x / 2, mo.curSize.y / 2));
            this.mLoadingSprite.setScale(0.5);
            this.addChild(this.mLoadingSprite);

            this.mLoaddingAnim = new cc.Sprite("res/ccs/hall_second/login/loading_0.png");
            this.mLoaddingAnim.setScale(0.2);
            this.mLoaddingAnim.setPosition(cc.p(mo.curSize.x / 2 + 5, mo.curSize.y / 2));
            this.addChild(this.mLoaddingAnim);

        }
    },

    onEnter : function(){
        this._super();

        cc.eventManager.addListener({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true,
            onTouchBegan: function(touch, event){
                cc.log("屏蔽了下层事件");
                return true;
            },
            onTouchMoved: function(touch, event){
            },
            onTouchEnded: function(touch, event){

            }
        }, this);
    },

    onExit : function(){
        mo.hallWaitLayer = null;
        this._super();
    },

    setCloseTime : function(time){
        if (time == undefined){
            time = 5;
        }

        if (mo.gameHelper.projectCode == 0){
            var spriteFrames = [];
            for (var i = 0; i <= 23; i++){
                var frame = cc.spriteFrameCache.getSpriteFrame("loding_" + i + ".png");
                if (frame == null){
                    break;
                }
                spriteFrames.push(frame);
            }
            var animation1 = new cc.Animation(spriteFrames);
            animation1.setDelayPerUnit(0.1);
            this.mLoadingSprite.runAction(cc.repeatForever(
                cc.sequence(
                    cc.animate(animation1)
                )
            ));
        }else{
            this.mLoadingSprite.runAction(cc.repeatForever(
                cc.sequence(
                    cc.rotateBy(1, -360)
                )
            ));

            var animation1 = new cc.Animation();
            for (var i = 0; i <= 1; i++){
                animation1.addSpriteFrameWithFile("res/ccs/hall_second/login/loading_" + i + ".png");
            }
            animation1.setDelayPerUnit(0.3);
            this.mLoaddingAnim.runAction(cc.repeatForever(
                cc.sequence(
                    cc.animate(animation1)
                )
            ));
        }

        this.runAction(cc.sequence(
            cc.delayTime(time),
            cc.removeSelf()
        ));
    },

    setHideWait : function(){
        this.mBg.setVisible(false);
        this.mLoadingSprite.setVisible(false);
        if (this.mLoaddingAnim != null){
            this.mLoaddingAnim.setVisible(false);
        }
    }
});

mo.hallWaitLayer = null;

mo.showWaitLayer = function(time){
    if (mo.hallWaitLayer == null){
        mo.hallWaitLayer = new HallWaitLayer();
        mo.hallWaitLayer.setCloseTime(time);
        mo.sceneMainLayer.addChild(mo.hallWaitLayer);
    }
};

mo.showWaitLayerNotVisible = function(time){
    if (mo.hallWaitLayer == null){
        mo.hallWaitLayer = new HallWaitLayer();
        mo.hallWaitLayer.setCloseTime(time);
        mo.hallWaitLayer.setHideWait();
        mo.sceneMainLayer.addChild(mo.hallWaitLayer);
    }
};

mo.closeWaitLayer = function(){
    if (mo.hallWaitLayer != null){
        mo.hallWaitLayer.removeFromParent();
        mo.hallWaitLayer = null;
    }
};